package com.reviewit.converters;

import javax.faces.component.UIComponent;
import javax.faces.context.FacesContext;
import javax.faces.convert.Converter;
import javax.faces.convert.ConverterException;
import javax.faces.convert.FacesConverter;

import com.reviewit.dao.DocLicenceDao;
import com.reviewit.model.DocLicence;

@FacesConverter(forClass = DocLicence.class, value = "docLicenceConverter")
public class DocLicenceConverter implements Converter {
	@Override
	public Object getAsObject(FacesContext arg0, UIComponent arg1, String value)
			throws ConverterException {
		if (value.trim().equals(""))
			return null;
		else
			return DocLicenceDao.getInstance().findOne("name", value);
	}
	
	@Override
	public String getAsString(FacesContext arg0, UIComponent arg1, Object value)
			throws ConverterException {
		if(value == null)
			return null;
		DocLicence dl = (DocLicence) value;
		return dl.getName();
	}
}